package fr.n7.khome.view.editors;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.AbstractCellEditor;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.TableCellEditor;

import fr.n7.khome.core.Request;
import fr.n7.khome.view.KHomeResultTableModel;

public class ImportanceEditor extends AbstractCellEditor implements TableCellEditor, MouseListener, ChangeListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8725286100277552330L;
	private JSlider slider;
	private KHomeResultTableModel<List<Request>> resultTableModel;

	boolean pressed;
	int row;
	int col;
	
	public ImportanceEditor(KHomeResultTableModel<List<Request>> resultTableModel){
		super();
		this.resultTableModel = resultTableModel;
		slider = new JSlider(0,10);
		slider.setMajorTickSpacing(5);
		slider.setMinorTickSpacing(1);
		slider.setSnapToTicks(true);
		slider.setPaintTicks(true);
		slider.setBackground(new Color(200,200,250));
		slider.setMaximumSize(null);
		slider.addMouseListener(this);
		slider.addChangeListener(this);
		this.pressed = false;
	}
	
	@Override
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean arg2, int row, int col) {
		//panel = new JPanel(new GridLayout(1,1));
		//panel.add(slider);
		this.row = row;
		this.col = col;
		slider.setValue((Integer)value);
		return slider;
	}
	
	@Override
	public Object getCellEditorValue() {
		return slider.getValue();
	}

	
	@Override
	public void mouseClicked(MouseEvent arg0) {}

	@Override
	public void mouseEntered(MouseEvent arg0) {}

	@Override
	public void mouseExited(MouseEvent arg0) {
		if(!pressed) stopCellEditing();
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		pressed = true;
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		pressed = false;
	}

	@Override
	public void stateChanged(ChangeEvent arg0) {
		resultTableModel.updateResultsWithImportance(row, col, slider.getValue());
	}
}
